package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

type UserInfo struct {
	Id                int
	Username          string
	Password          string
	ProfilePictureUrl string
	Nickname          string
}

func main() {
	// 用户名：密码@tcp(ip:port)/数据库?charset=utf8&parseTime=True&loc=Local
	db, err := gorm.Open("mysql", "root:happy15699@tcp(localhost:3306)/entry_task?charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		panic(err)
	}
	defer db.Close() //关闭空闲连接

	//数据结构同步
	//db.AutoMigrate(&UserInfo{})

	// 新增
	//db.Create(UserInfo{Id: 12, Username: "zhangsan", Password: "123", ProfilePictureUrl: "aaaa.jpg", Nickname: "san"})
	//db.Create(UserInfo{Id: 13, Username: "lisi", Password: "123", ProfilePictureUrl: "aaaa.jpg", Nickname: "si"})

	//查询
	var userInfo UserInfo
	//db.First(&userInfo, 12)
	//fmt.Println(userInfo)

	//查询2
	db.First(&userInfo, "username=?", "zhangsan")
	fmt.Println(userInfo)

	// 更新
	//db.Model(&userInfo).Update("nickname", "nihao")
	//db.Model(&userInfo).Update("username", "zhangsan01")

	// 删除
	db.Delete(&userInfo)
	//fmt.Println(userInfo)

}
